﻿using AutoMapper;
using MediatR;
using UserLicense.Applicationes.Command;
using UserModels;
using UserRepositorys.Intface;

namespace UserLicense.Applicationes.CommandHandler
{
    public class MenuTreeAddCommandHandler : IRequestHandler<MenuTreeAddCommand, int>
    {
        private readonly IMenuRepository _menuRepository;
        private readonly IMapper _mapper;
        public MenuTreeAddCommandHandler(IMenuRepository menuRepository,IMapper mapper)
        {
            _menuRepository = menuRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Handle(MenuTreeAddCommand request, CancellationToken cancellationToken)
        {
            MenuInfoModel model= _mapper.Map<MenuInfoModel>(request);
            model.CreateBy = "admin";
            model.CreateTime = DateTime.Now;
            model.MenuUrl ="//";
            model.UpdateTime = DateTime.Now;
            model.UpdateBy = "admin";
            return await _menuRepository.AddMenus(model);
        }
    }
}
